<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册界面</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        网页聊天
    </div>
    <!-- 主窗口区 -->
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name= "username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <div class="row">
                <button id="submit2" onclick="register()">注册</button>
            </div>
        </div>

        <!-- 引入 jquery -->
        <script src="js/jquery.min.js"></script>
        <!-- 前后端交互 -->
        <script>
            function register() {
            $.ajax({
                type: "post",
                url: "/user/register",
                contentType: "application/json",
                data: JSON.stringify ({
                    // 将对象或值转换为 json字符串
                    username: $("#username").val(),
                    password: $("#password").val()
                }),
                success: function(result) {
                    if(result != null && result.code == "SUCCESS") {
                        // 密码正确
                        localStorage.setItem("loginUserId",result.data.userId);
                        localStorage.setItem("loginUserName",result.data.username);
                        alert("注册成功，请进行登录");
                        location.href = "login.html";
                    }else {
                        alert(result.errMsg);
                    }
                }
            });
        }
        </script>
    </div>
</body>
</html>